/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.mobile.api.message.service.PushMessageService.java 
 * <p>
 * 类产生时间: 14-4-30 上午9:18
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 掌上公安应用平台
 * <p>
 * 所在部门: 开发部--手持技术部
 * <p>
 * <p>
 */
package com.oceansoft.mobile.econsole.modules.message.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import cn.jpush.api.push.PushResult;

import com.oceansoft.mobile.econsole.common.constant.ConfigInfo;
import com.oceansoft.mobile.econsole.modules.message.bizz.V3Handler;
import com.oceansoft.mobile.econsole.modules.message.entity.ExceptionCommonBase;
import com.oceansoft.mobile.econsole.modules.message.entity.MessageContent;
import com.oceansoft.mobile.econsole.modules.message.entity.PushMessage;
import com.oceansoft.mobile.econsole.modules.message.persistence.IMessageMapper;
import com.oceansoft.mobile.econsole.modules.message.util.Utils;

/**
 * 消息推送控制器
 *
 * @author: zxh
 * @time: 14-4-30 上午9:18
 */
@Service
public class PushMessageService {
    @Resource
    protected ConfigInfo configInfo;

    @Resource
    private IMessageMapper messageMapper;
    
    Logger logger = Logger.getLogger(PushMessageService.class);

    @Transactional
    public String saveMessage(String title, PushMessage.PushType pushType, String messageGroup, String alias, String contentStr, PushMessage.ContentType contentType, String extra,String platForm) throws ExceptionCommonBase {
        String content = "";
        String result = "";
        PushMessage message = null;
        if (StringUtils.isEmpty(pushType)) {
            result = "缺少发送的对象";
        }
        if (StringUtils.isEmpty(contentStr)) {
            result = "缺少内容";
        }
        if (StringUtils.isEmpty(contentType)) {
            result = "缺少消息的种类";
        }
        content = new MessageContent(title, contentStr, contentType.getCode(), extra).toJson();
        if (pushType.equals(PushMessage.PushType.BROADCAST)) {
            message = new PushMessage(Utils.getCurrNo(), pushType.getType(), "", content, "test");
        } else if (pushType.equals(PushMessage.PushType.GROUP)) {
            if (StringUtils.isEmpty(messageGroup)) {
                result = "缺少群组名称";
            }
            message = new PushMessage(Utils.getCurrNo(), pushType.getType(), messageGroup, content, "test");
        } else if (pushType.equals(PushMessage.PushType.ALIAS)) {
            if (StringUtils.isEmpty(alias)) {
                result = "缺少别名";
            }
            message = new PushMessage(Utils.getCurrNo(), pushType.getType(), alias, content, "test");
        } else {
            result = "发送对象错误";
        }
        if (message != null) {
            message.setCreate_time(new Date());
            message.setPlatform(platForm);
            messageMapper.add(message);
       //     pushMessage();
        }
        return result;
    }

//    public void pushMessage() {
//        List<PushMessage> msgList = messageMapper.findUnsendMsg();
//        List<Integer> idlist = null;
//        System.out.println("[scheduled task]==================消息推送开始==================");
//        if (null != msgList && msgList.size() > 0) {
//            idlist = new ArrayList<Integer>();
//            for (PushMessage msg : msgList) {
//            	if("android".equals(msg.getPlatform())){
//            		msg.setPlatform("0");
//            	}
//            	if("ios".equals(msg.getPlatform())){
//            		msg.setPlatform("1");
//            	}
//            	if("all".equals(msg.getPlatform())){
//            		msg.setPlatform("2");
//            	}
//                try {
//                    System.out.println("执行ID:" + msg.getId());
//                    Utils.httpPost(configInfo.getPushServerHost(), null, Utils.toParamFromBean(msg));
//                    idlist.add(msg.getId());
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//            messageMapper.updateMsgSendStatus(idlist);
//        }
//        System.out.println("[scheduled task]==================消息推送结束==================");
//    }
    
    public void pushMessage() {
        List<PushMessage> msgList = messageMapper.findUnsendMsg();
        List<Integer> idlist = null;
        System.out.println("[scheduled task]==================消息推送开始==================");
        if (null != msgList && msgList.size() > 0) {
            idlist = new ArrayList<Integer>();
            for (PushMessage msg : msgList) {
            	
            	 try {
                     logger.info("执行ID:" + msg.getId());

                     V3Handler handler = new V3Handler(msg);
                     PushResult result = handler.push();

                     logger.info("=====>result:"+result);
                     if(result.msg_id == 0 || result.sendno == 0){
                         logger.info("=====消息推送失败,消息ID: "+msg.getId());
                     }else{
                         idlist.add(msg.getId());
                     }
                 } catch (Exception e) {
                     e.printStackTrace();
                 }
             }
             if(idlist.size() > 0){
                 messageMapper.updateMsgSendStatus(idlist);
             }
         }
         System.out.println("[scheduled task]==================消息推送结束==================");

    }
}
